#include <bits/stdc++.h>

#define int long long

using namespace std;

void solve()
{
    int n;
    cin >> n;
    vector<int> arr(n + 10);
    for(int i = 1; i <= n; i ++) cin >> arr[i];
    vector<int> dp(n + 10);
    unordered_map<int, vector<int>> srr;
    for(int i = 1; i <= n; i ++)
    {
        srr[arr[i]].push_back(i);
        if(srr[arr[i]].size() >= arr[i])
        {
            int sz = srr[arr[i]].size() - arr[i];
            dp[i] = max(dp[i - 1],dp[srr[arr[i]][sz] - 1] + arr[i]);
        }
        else dp[i] = dp[i - 1];
    }
    // for(int i = 1; i <= n; i ++)
    // {
    //     cout << dp[i] << ' ';
    // }
    cout << dp[n] << endl;
}

signed main()
{
    int t;
    cin >> t;
    while(t --)
    {
        solve();
    }
}

